﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PortalCurriculo.Util.CustomControls
{
    public class Popup : Panel
    {
        public string CssClassDivOut { private get; set; }
        public string CssClassDivInside { private get; set; }
        public Color PanelOutColor { private get; set; }
        public Color PanelInsideColor { private get; set; }

        protected override void OnLoad(EventArgs e)
        {
            //SetOpenPopupWhenChangeControl();

            if (Page.IsPostBack) return;
            Close();

            base.OnLoad(e);
        }

        private void SetOpenPopupWhenChangeControl()
        {
            #region DropDownList
            var controls = GetControls<DropDownList>();

            foreach (DropDownList control in controls)
                control.SelectedIndexChanged += OnChangeControl;
            #endregion

            //#region CheckBox
            //controls = GetControls<CheckBox>();

            //foreach (CheckBox control in controls)
            //    control.CheckedChanged += OnChangeControl;

            //controls = GetControls<CheckBoxList>();

            //foreach (CheckBoxList control in controls)
            //    control.SelectedIndexChanged += OnChangeControl;
            //#endregion

            #region RadioButton
            controls = GetControls<RadioButton>();

            foreach (RadioButton control in controls)
                control.CheckedChanged += OnChangeControl;

            controls = GetControls<RadioButtonList>();

            foreach (RadioButtonList control in controls)
                control.SelectedIndexChanged += OnChangeControl;
            #endregion
        }

        public void OnChangeControl(object sender, EventArgs e)
        {
            Show();
        }

        private void LoadScreen()
        {
            //Controls.Add(CreatePanelClose());

            var panelOut = new Panel
            {
                CssClass = CssClassDivOut,
                ID = "pnlOut",
                BackColor = PanelInsideColor.IsEmpty ? Color.Black : PanelOutColor
            };
            panelOut.Attributes.Add("style", "position: absolute; filter: alpha(opacity=90);top: 0;left: 0;");
            Parent.Controls.Add(panelOut);

            CssClass = CssClassDivInside;
            BackColor = PanelInsideColor.IsEmpty ? Color.White : PanelInsideColor;
            //Attributes.Add("style", "position: fixed; border: 2px solid black; padding: 10px 12px 10px 12px; elevation:above;z-index:10;");
            Attributes.Add("style", "position: fixed; elevation:above;z-index:10;");
        }

        public Panel CreatePanelClose()
        {
            var panel = new Panel { Height = new Unit(25, UnitType.Pixel), };
            var linkButton = new LinkButton { Text = "X", CausesValidation = false };
            linkButton.Click += ButtonClose_Click;
            linkButton.Attributes.Add("Style", "float:right;");

            panel.Controls.Add(linkButton);
            return panel;
        }

        public void Show()
        {
            ShowPanel();
            LoadScreen();

            Control panelOut = (from Control control in Parent.Controls
                                where control.ID != null && control.ID.Equals("pnlOut")
                                select control).FirstOrDefault();

            var script = new StringBuilder();

            script.Append("var divOut = document.getElementById('").Append(panelOut.ClientID).AppendLine("');");
            script.Append("var divInside = document.getElementById('").Append(ClientID).AppendLine("');");

            script.AppendLine("var width = 0;");
            script.AppendLine("var height = 0;");

            script.AppendLine("divOut.style.width = '9000%';");
            script.AppendLine("divOut.style.height = '9000%';");
            script.AppendLine("document.body.style.overflow = 'hidden';");
            script.AppendLine("document.body.scroll = 'no';");

            script.AppendLine("width = window.innerWidth;");
            script.AppendLine("height = window.innerHeight;");
           
            script.AppendLine("var top =  ((height - divInside.offsetHeight) / 2 * 100) / height;");
            script.AppendLine("var left = ((width - divInside.offsetWidth) / 2 * 100) / width;");
            script.AppendLine("divInside.style.top = top + '%';");
            script.AppendLine("divInside.style.left = left + '%';");

            RunScript(script.ToString());
        }

        private void ShowPanel()
        {
            Attributes.Remove("style");
        }

        private void RunScript(string script)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), script, true);
        }

        protected void ButtonClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        public void Close()
        {
            Attributes.Remove("style");
            Attributes.Add("style", "display:none;");

            RunScript("document.body.style.overflow = ''; document.body.scroll = 'yes';");
        }

        public void ClearFields()
        {
            var controls = GetControls<TextBox>();

            foreach (var textBox in controls)
                ((TextBox)textBox).Text = string.Empty;

            controls = GetControls<DropDownList>();

            foreach (DropDownList dropDownList in controls)
                dropDownList.SelectedIndex = 0;

            controls = GetControls<CheckBox>();

            foreach (CheckBox checkBox in controls)
                checkBox.Checked = false;
        }

        private List<Control> GetControls<T>()
        {
            var controls = new ArrayList();
            controls.AddRange(Controls);

            return (from Control control in controls
                    where control.GetType().Equals(typeof(T))
                    select control).ToList();
        }
    }
}
